class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int n = gas.size();
        for (int i = 0; i < n; i++)
        {
            int rest = 0;
            for (int step = 0; step < n; step++)
            {
                int index = (step + i) % n;
                rest = rest + gas[index] - cost[index];
                if (rest < 0)
                {
                    i += step;
                    break;
                }
            }
            if (rest >= 0)
            {
                return i;
            }
        }
        return -1;
    }
};